package de.roskenet.clou.controller;

import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.event.map.StateChangeEvent;

import de.roskenet.clou.dao.ClouDayDAO;
import de.roskenet.clou.model.ClouDay;

public class ClouDayController {
	
	private ClouDayDAO clouDayDAO;
	private int selectedTag;
	private String headText = new String("");
	private String descrText = new String("");
	
	public void init() {
		// Setzte initiale Werte (Standard ist heute!)
		// Calendar cal = new GregorianCalendar();
		selectedTag = new GregorianCalendar().get(Calendar.DAY_OF_WEEK);
		populateFields();
	}
	
	public String getHeadText() {
		return headText;
	}

	public void setHeadText(String headText) {
		this.headText = headText;
	}

	public String getDescrText() {
		return descrText;
	}

	public void setDescrText(String descrText) {
		this.descrText = descrText;
	}

	public int getSelectedTag() {
		return selectedTag;
	}

	public void setSelectedTag(int selectedTag) {
		this.selectedTag = selectedTag;
	}

	public ClouDayDAO getClouDayDAO() {
		return clouDayDAO;
	}

	public void setClouDayDAO(ClouDayDAO clouDayDAO) {
		this.clouDayDAO = clouDayDAO;
	}

	public void populateFields() {
		ClouDay theDay = clouDayDAO.getByDay(selectedTag);
		headText=theDay.getTitle();
		descrText = theDay.getDescr();
		
	}
	public void saveOrUpdate() {
		ClouDay theDay = new ClouDay();
		theDay.setTag(selectedTag);
		theDay.setTitle(headText);
		theDay.setDescr(descrText);
		clouDayDAO.saveOrUpdate(theDay);
		FacesMessage msg = new FacesMessage("Erfolgreich", "Texte wurden gespeichert.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);
	}
	
	public void onChange() {
		populateFields();
	}
}
